<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="登录地点" prop="location">
        <el-input v-model="queryParams.location" placeholder="请输入登录地址" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="用户名称" prop="username">
        <el-input v-model="queryParams.username" placeholder="请输入用户名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="登录状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择登录状态" clearable>
          <el-option v-for="item in options" :key="item" :label="item" :value="item" />
        </el-select>
      </el-form-item>
      <el-form-item label="登录时间">
        <el-date-picker style="width: 191px;" v-model="dateRange" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
      </el-form-item>
      <el-form-item>
        <el-button v-hasPermi="['system:login-log:list']" type="primary" icon="el-icon-search" size="small"
          @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="danger" v-hasPermi="['system:login-log:delete']" :disabled="multiple" plain
          icon="el-icon-delete" size="small" @click="handleDelete">批量删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" v-hasPermi="['system:login-log:export']" plain icon="el-icon-download" size="small"
          @click="handleExport">导出</el-button>
      </el-col>
    </el-row>

    <el-table style="margin-top: 20px;" v-loading="loading" :default-sort="{prop: 'loginTime', order: 'descending'}"
      ref="tables" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="45" align="center" />
      <el-table-column label="用户名称" align="center" prop="username" :show-overflow-tooltip="true" />
      <el-table-column label="登录地址" align="center" prop="ip" width="130" :show-overflow-tooltip="true" />
      <el-table-column label="登录地点" align="center" prop="location" :show-overflow-tooltip="true" />
      <el-table-column label="浏览器" align="center" prop="browser" :show-overflow-tooltip="true" />
      <el-table-column label="操作系统" align="center" prop="os" />
      <el-table-column label="登录状态" align="center" prop="status" />
      <el-table-column label="登录信息" align="center" prop="msg" :show-overflow-tooltip="true" />
      <el-table-column sortable label="登录日期" align="center" prop="loginTime" width="180" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="text" v-hasPermi="['system:login-log:delete']" icon="el-icon-delete" size="mini"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination style="float:right;margin-top: 20px;" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" background :current-page="queryParams.currentPage"
      :page-sizes="[5,10,50,100]" :page-size="queryParams.pageSize" layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
  import {
    listLoginLog,
    delLoginLog
  } from "@/api/system/login-log";

  export default {
    name: "LoginLog",
    data() {
      return {
        loading: true,
        options: ["成功", "失败"],
        // 选中数组
        ids: [],
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 表格数据
        list: [],
        // 日期范围
        dateRange: [],
        queryParams: {
          currentPage: 1,
          pageSize: 10,
          location: '',
          username: '',
          status: '',
          beginTime: '',
          endTime: ''
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      handleCurrentChange(val) {
        this.queryParams.currentPage = val;
        this.getList();
      },
      handleSizeChange(val) {
        this.queryParams.pageSize = val;
        this.getList();
      },
      /** 查询登录日志列表 */
      getList() {
        this.loading = true
        if (this.dateRange instanceof Array && this.dateRange.length == 2) {
          this.queryParams.beginTime = this.dateRange[0]
          this.queryParams.endTime = this.dateRange[1]
        }
        listLoginLog(this.queryParams).then(
          response => {
            this.list = response.data.records;
            this.total = response.data.total;
            this.loading = false
          });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.currentPage = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams = {}
        this.dateRange = []
        this.handleQuery();
      },
      /** 多选框选中数据 */
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.multiple = !selection.length
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        let ids = row.id || this.ids
        this.$confirm(`此操作删除id为【${ids}】的日志, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delLoginLog(ids).then(() => {
            this.getList();
            this.$message.success("删除成功");
          })
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('/system/login-log/export', {
          ...this.queryParams
        }, '登录日志表.xlsx')
      }
    }
  }
</script>
